package com.zksb.entity;

import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;

import java.io.Serializable;

public class Result implements Serializable {
    private int code;
    private String status;
    private String message;
    private Object data;

    public final static String ERROR = "error";
    public final static String FAIL = "fail";
    public final static String SUCCESS = "success";

    public Result() { }

    /**
     * 创建Result对象
     * @param code    HTTP状态码
     * @param status  影响状态: error, fail, success
     * @param message 错误信息，正常则为空
     * @param data    响应数据，出现异常时这里可以是错误消息，也可以是null
     */
    public Result(int code, String status, String message, Object data) {
        this.code = code;
        this.status = status;
        this.message = message;
        this.data = data;
    }

    //返回404结果
    public static Result notFound(String message) {
        message = StringUtils.isEmpty(message) ? HttpStatus.NOT_FOUND.name() : message;
        return new Result(HttpStatus.NOT_FOUND.value(), ERROR, message, null);
    }

    //返回操作成功的结果
    public static Result success(Object data) {
        return new Result(HttpStatus.OK.value(), SUCCESS, "", data);
    }

    //返回操作失败的结果
    public static Result fail(String message, int code) {
        return new Result(code, FAIL, message, null);
    }

    //返回操作错误的结果
    public static Result error(String message, int code) {
        return new Result(code, ERROR, message, null);
    }
    //省略getter/setter方法


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
